package com.gx.smart.building.ui.swap.fragment

import android.content.Intent
import android.view.Gravity
import android.view.View
import android.view.ViewGroup
import com.blankj.utilcode.util.TimeUtils
import com.gx.smart.base.BaseAdapter
import com.gx.smart.base.BaseMVVMRefreshFragmentV2
import com.gx.smart.building.R
import com.gx.smart.building.databinding.FragmentSwapRecordBinding
import com.gx.smart.building.ui.common.timepicker.TimePicker
import com.gx.smart.building.ui.swap.SwapDetailActivity
import com.gx.smart.building.ui.swap.adapter.SwapRecordAdapter
import com.gx.smart.building.ui.swap.viewmodel.SwapRecordViewModel
import com.huishi.gx.ib.grpc.lib.ib.admin.schedule.v1.RecordDto
import com.scwang.smartrefresh.layout.api.RefreshLayout
import razerdp.basepopup.QuickPopupBuilder
import razerdp.basepopup.QuickPopupConfig

class SwapRecordFragment : BaseMVVMRefreshFragmentV2<SwapRecordViewModel>(),
    BaseAdapter.OnItemClickListener<RecordDto> {
    private lateinit var viewBinding: FragmentSwapRecordBinding
    private lateinit var adapter: SwapRecordAdapter
    private var beginTime: Long = 0L
    private var endTime: Long = 0L

    companion object {
        fun newInstance() = SwapRecordFragment()
    }

    override fun onBindLayout(): Int = R.layout.fragment_swap_record

    override fun getToolbarTitle(): String = ""

    override fun enableToolbar(): Boolean = false

    override fun onBindViewModel(): Class<SwapRecordViewModel> = SwapRecordViewModel::class.java

    override fun initContentView(root: ViewGroup) {
        viewBinding = FragmentSwapRecordBinding.inflate(layoutInflater, root, true)
        viewBinding.filter.setOnClickListener {
            QuickPopupBuilder.with(context)
                .contentView(R.layout.inspection_popwindow_layout)
                .config(
                    QuickPopupConfig()
                        .gravity(Gravity.BOTTOM or Gravity.CENTER_VERTICAL)
                        .withClick(
                            R.id.all,
                            { v1: View? ->
                                all()
                            }, true
                        )
                        .withClick(
                            R.id.time,
                            { v1: View? ->
                                timePicker()
                            }, true
                        )
                )
                .show(it)
        }
    }

    override fun initContent() {
        super.initContent()
        adapter = SwapRecordAdapter(requireContext())
        adapter.setItemClickListener(this)
        initView(viewBinding.recyclerView, adapter, viewBinding.refreshLayout)
    }

    override fun onRefresh(refreshlayout: RefreshLayout) {
        super.onRefresh(refreshlayout)
        doAction()
    }

    override fun onLoadMore(refreshLayout: RefreshLayout) {
        super.onLoadMore(refreshLayout)
        doAction()
    }


    open fun doAction() {
        viewModel.queryAppScheduleRecordList(this.currentPage, beginTime, endTime)
            .observe(viewLifecycleOwner) { list ->
                handleResult(list)
            }
    }

    private fun all() {
        viewBinding.filterContent.text = ""
        beginTime = 0L
        endTime = 0L
        if (this::viewBinding.isInitialized) {
            viewBinding.refreshLayout.autoRefresh()
        }
    }

    override fun onResume() {
        super.onResume()
        viewBinding.refreshLayout.autoRefresh()
    }

    private fun timePicker() {
        val deadlinePicker = TimePicker(
            requireContext()
        ) { startTime, endTime ->
            viewBinding.filterContent.text = TimeUtils.millis2String(
                startTime,
                "yyyy-MM-dd"
            ) + "-" + TimeUtils.millis2String(endTime, "yyyy-MM-dd")
            this.beginTime = startTime
            this.endTime = endTime
            if (this::viewBinding.isInitialized) {
                viewBinding.refreshLayout.autoRefresh()
            }
        }
        deadlinePicker.setIsLoop(false)
        deadlinePicker.setTouchMode(true)
        deadlinePicker.setMode(TimePicker.MODE.YMD)
        deadlinePicker.show()
    }

    override fun onItemClick(item: RecordDto, position: Int) {
        //App专用 1我的交班 2我的接班
        val intent = Intent(requireActivity(), SwapDetailActivity::class.java)
        intent.putExtra("id", item.id)
        startActivity(intent)
    }

}